//
//  JSSubTableView.m
//  NiubilityProject
//
//  Created by Apple on 2018/3/19.
//  Copyright © 2018年 Apple. All rights reserved.
//

#import "JSSubTableView.h"

@implementation JSSubTableView

- (void)initTableView{
    
}
- (instancetype) initWithFrame:(CGRect)frame withStyle:(UITableViewStyle )style{
    if (self = [super initWithFrame:frame]) {
        [self didInitializedWithStyle:style];
        
    }
    return self;
}
- (instancetype)initWithStyle:(UITableViewStyle)style {
    if (self = [super initWithFrame:CGRectZero]) {
        [self didInitializedWithStyle:style];
    }
    return self;
}

- (void)didInitializedWithStyle:(UITableViewStyle)style {
    _style = style;
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:style];
    _tableView.tableFooterView = [UIView new];
    [self addSubview:_tableView];
    _tableView.dataSource = self;
    _tableView.delegate   = self;
    _tableView.emptyDataSetSource = self;
    _tableView.emptyDataSetDelegate = self;
    //    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    _tableView.mj_header = [CustomGifRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    // 自动改变透明度
    _tableView.mj_header.automaticallyChangeAlpha = YES;
    //    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    _tableView.mj_footer = [CustomGifRefreshFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    _tableView.mj_footer.automaticallyChangeAlpha = YES;
    _isHidenHeaderRefresh = YES;
    _isHidenFooterRefresh = YES;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView.mj_footer endRefreshingWithNoMoreData];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    self.dataService = [[JSBaseDataService alloc] init];
    self.dataService.delegate = self;
}
#pragma mark -------UITableViewDataSource\UITableViewDelegate---------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return kRealValue(44);
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"basecell"];
    if(!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"basecell"];
    }
    return cell;
}
- (void)headerRefresh{
    [self.dataService refrenshData];
}
- (void)footerRefresh{
    [self.dataService nextData];
}
- (void)setIsHidenFooterRefresh:(BOOL)isHidenFooterRefresh{
    _isHidenFooterRefresh = isHidenFooterRefresh;
    if (isHidenFooterRefresh) {
        self.tableView.mj_footer = nil;
    }else{
        self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
        self.tableView.mj_footer.automaticallyChangeAlpha = YES;
    }
}
- (void)setIsHidenHeaderRefresh:(BOOL)isHidenHeaderRefresh{
    _isHidenHeaderRefresh = isHidenHeaderRefresh;
    if (_isHidenHeaderRefresh) {
        self.tableView.mj_header = nil;
    }else{
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
        // 自动改变透明度
        self.tableView.mj_header.automaticallyChangeAlpha = YES;
    }
}
- (void)footerRefreshType{
    id child = self.dataSource.firstObject;
    NSInteger total = 0;
    if ([child isKindOfClass:[NSArray class]]) {
        for (NSArray *ar in self.dataSource) {
            total += ar.count;
        }
        if (total < self.dataService.page * 10) {
            [self.tableView.mj_footer resetNoMoreData];
        }else{
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    }else{
        if (self.dataSource.count < self.dataService.page * 10) {
            [self.tableView.mj_footer resetNoMoreData];
        }else{
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    }
}

-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = @[].mutableCopy;
    }
    return _dataSource;
}
- (void)dealloc {
    _tableView.delegate = nil;
    _tableView.dataSource = nil;
}
#pragma mark - DZNEmptyDataSetSource
// 空白页图片
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
    return [UIImage imageNamed:@"icon_empty"];
}
// 标题文本，富文本样式
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *text = @"暂无数据";
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:14.0f],
                                 NSForegroundColorAttributeName: JS_ColorWithRGB(222, 222, 222)};
    
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}
// 是否 允许上下滚动，默认NO
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{
    return YES;
}

#pragma mark -------JSDataServiceDelegate-----------------
// 请求数据回调
-(void)dataServiceDidUpdate:(JSBaseDataService *)service data:(id)data error:(NSError *)error{
    NSLog(@"data:%@ -- error:%@", data,error);
    [JSLoadingView hidenLoadingForView:self];
    //    [self footerRefreshType];
}
-(NSString *)dataServiceUrlSting{
    // 请求所需url
    return BASE_URL;
}
-(NSMutableDictionary *)dataServiceParameter{
    // 请求所需参数
    return @{}.mutableCopy;
}
// 结束下拉刷新
-(void)overHeaderRefrensh{
    [self.tableView.mj_header endRefreshing];
}
/*!
 结束上拉加载更多
 
 isResstingData : 是否重置数据
 */
-(void)overFooterRefrensh:(BOOL)isResstingData{
    if (isResstingData) {
        NSMutableArray *temp = self.dataSource;
        [self.dataSource removeAllObjects];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            [temp class];
        });
    }
    [self.tableView.mj_footer endRefreshing];
}
-(NSInteger)dataServicePageNumber{
    return 0;
}
@end
